package com.pcbly.web.persistence.crm;

/**
 * @author carl.song
 * Copyright (c) 2010 Lightsoft, International.
 * All rights reserved.
 *
 */
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.bean.crm.SysInitSetBean;
import com.pcbly.web.persistence.HibernateBaseDao;
import com.pcbly.web.components.hibernate.support.Page;

@SuppressWarnings("unchecked")
public class SysInitSetHibernateDao extends HibernateBaseDao<SysInitSetBean>
		implements SysInitSetDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7272533181564213400L;

	protected Class<SysInitSetBean> getEntityClass() {
		return SysInitSetBean.class;
	}

	public SysInitSetHibernateDao() {
		super();
	}

	@Override
	public SysInitSetBean saveSysInitSetBean(SysInitSetBean crm)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug(crm.getId());
		}
		String id = (new GUID()).toString();
		crm.setId(id);
		getHibernateTemplate().save(crm);
		return crm;
	}

	@Override
	public void removeSysInitSetBeanByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		ids = ids.replaceAll(",", "','");
		String hsql = "update t_crm_sysinitset set RECYCLED='1' where ID in( '"
				+ ids + "')";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	@Override
	public void removeSysInitSetBeanById(String id) throws Exception {
		// TODO Auto-generated method stub
		id = id.replaceAll(",", "','");
		String hsql = "update t_crm_sysinitset set RECYCLED='1' where ID= '"
				+ id + "'";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	public Page<SysInitSetBean> findSysInitSetBeanByCond(SysInitSetBean bean,
			Page<SysInitSetBean> page, String orderByName, boolean asc)
			throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from t_crm_sysinitset where RECYCLED='0' ");
		if (!(bean == null)) {
		}
		if (asc) {
			// criteria.addOrder(Order.asc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" ");
		} else {
			// criteria.addOrder(Order.desc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" desc ");
		}
		Object obj = getSession().createSQLQuery(
				"select count(t.ID) from (" + sb + ")t").uniqueResult();
		if (obj == null) {
			obj = "0";
		}

		page.setTotalCount(Integer.parseInt(String.valueOf(obj)));
		if (page.getTotalCount() > 0) {
			int first = page.getFirst();
			int lpage = first + page.getPageSize();
			if (lpage > page.getTotalCount()) {
				lpage = page.getTotalCount();
			}
			List<SysInitSetBean> beans = getSession().createSQLQuery(
					sb.toString()).addEntity(SysInitSetBean.class).list()
					.subList(first, lpage);
			page.setResult(beans);
		}
		return page;
	}

	@Override
	public void setSysInitSetBean(SysInitSetBean bean) throws Exception {
		// TODO Auto-generated method stub
		String hsql = "update t_crm_sysinitset set RECYCLED='1' ";
		Session session=getSession();
		session.createSQLQuery(hsql).executeUpdate();
		bean.setCreateDate(new Date());
		bean.setCswcrq(new Date());
		bean.setQyrq(new Date());
		bean.setCzDate(new Date());
		bean.setRecycled("0");
		bean.setId((new GUID()).toString());
		bean.setFlag("0");
		session.save(bean);
	}
	public void clearData()throws Exception{
		List<String> list=new ArrayList<String>();
		list.add("delete from t_crm_accountdzd");//对账单
		list.add("delete from t_crm_accountqc");//账户期初
		list.add("delete from t_crm_accounts");//账户
		list.add("delete from t_crm_bxd");//报修单
		list.add("delete from t_crm_bxdproduct");//报修单商品
		list.add("delete from t_crm_bxfhd");//报修返还单
		list.add("delete from t_crm_bxfhdproduct");//报修返还单商品
		list.add("delete from t_crm_cgfk");//采购付款
		list.add("delete from t_crm_cgfkdesc");//采购付款明细
		list.add("delete from t_crm_cgthd");//采购退货单
		list.add("delete from t_crm_cgthdproduct");//采购退货单产品
		list.add("delete from t_crm_chtj");//存货调价
		list.add("delete from t_crm_chtjdesc");//存货调价明细
		list.add("delete from t_crm_ckd");//出库单
		list.add("delete from t_crm_ckdproduct");//出库单产品
		
		list.add("delete from t_crm_clients");//往来客户
		list.add("delete from t_crm_clientsfollow");//客户跟进计划
		list.add("delete from t_crm_clientslinkman");//客户联系人
		list.add("delete from t_crm_clientwlinit");//客户往来初始		
		list.add("delete from t_crm_cxd");//商品拆卸单
		list.add("delete from t_crm_cxdproduct");//商品拆卸单明细
		
		list.add("delete from t_crm_dbsq");//调拨申请
		list.add("delete from t_crm_dbsqproduct");//调拨申请相关产品
		list.add("delete from t_crm_fhkhd"); //返还客户单
		list.add("delete from t_crm_fhkhdproduct");//返还客户单商品
		
		list.add("delete from t_crm_flinkman");
		list.add("delete from t_crm_flx");
		list.add("delete from t_crm_freadsms");
		list.add("delete from t_crm_fsendsms");
		
		list.add("delete from t_crm_fxdd");//分销订单
		list.add("delete from t_crm_fysq"); //费用申请
		
		list.add("delete from t_crm_fytype");
		
		list.add("delete from t_crm_jhd");//进货单
		list.add("delete from t_crm_jhdproduct");//进货单相关产品
		list.add("delete from t_crm_jjd");//接件单
		list.add("delete from t_crm_jjdproduct");//接件单商品
		list.add("delete from t_crm_kcpd");//库存盘点
		list.add("delete from t_crm_kcpddesc");//库存盘点明细
		list.add("delete from t_crm_kcpdyktbl");//库存盘点盈余
		list.add("delete from t_crm_kfdb");//库房调拨
		list.add("delete from t_crm_kfdbproduct");//库房调拨相关产品
		list.add("delete from t_crm_lsd");//零售单
		list.add("delete from t_crm_lsdproduct");//零售单相关产品
		
		list.add("delete from t_crm_lsthd");
		list.add("delete from t_crm_lsthdproduct");
		
		list.add("delete from t_crm_lsysk"); //零售预收款
		
		list.add("delete from t_crm_mailset");
		list.add("delete from t_crm_nbzz");
		
		list.add("delete from t_crm_pgd");//派工单
		list.add("delete from t_crm_postype");//POS机设置
		list.add("delete from t_crm_product");//产品表
		list.add("delete from t_crm_productkc"); //产品库存
		list.add("delete from t_crm_productkcinit");//库存期初设置
		list.add("delete from t_crm_productkind");//产品类别
		
		list.add("delete from t_crm_provider");
		
		list.add("delete from t_crm_pz");//往来调账（平账）
		list.add("delete from t_crm_qtsr");//其它收入
		list.add("delete from t_crm_qtzc");//其它支出
		list.add("delete from t_crm_rkd"); //入库单
		list.add("delete from t_crm_rkdproduct");//入库单相关产品
		list.add("delete from t_crm_sendmsglog");//短信发送日志
		
		list.add("delete from t_crm_serialnumflow");//序列号流转过程表
		list.add("delete from t_crm_serialnummng");//序列号管理表
		list.add("delete from t_crm_sfd");//售后服务单
		list.add("delete from t_crm_shkc");//售后库存
		list.add("delete from t_crm_shserialnumflow");//售后库存序列号流转记录
		
		list.add("delete from t_crm_sjzdjbxx");//数据字典基本信息
		list.add("delete from t_crm_sjzdxmxx");//数据字典子项信息
		
		list.add("delete from t_crm_storehouse"); //仓库
		list.add("delete from t_crm_sysinitset");//系统初始相关设置
		list.add("delete from t_crm_sysmsg");
		
		list.add("delete from t_crm_thd");//退货单
		list.add("delete from t_crm_thdproduct");//退货单相关产品
		
		list.add("delete from t_crm_txfk");//摊销付款
		list.add("delete from t_crm_txfkdesc"); //摊销付款明细
		
		list.add("delete from t_crm_wxcld");//维修处理单
		list.add("delete from t_crm_wxcldproduct");//维修处理单商品
		
		list.add("delete from t_crm_wxrkd");//维修入库单
		list.add("delete from t_crm_wxrkdproduct");//维修入库单商品
		
		list.add("delete from t_crm_xsd");//销售订单
		list.add("delete from t_crm_xsdproduct");//销售订单相关产品
		
		list.add("delete from t_crm_xssk");//销售收款
		list.add("delete from t_crm_xsskdesc");//销售收款明细
		
		list.add("delete from t_crm_xxfbnbgg");
		
		list.add("delete from t_crm_ykck");//移库出库
		list.add("delete from t_crm_ykckproduct");//移库出库明细
		
		list.add("delete from t_crm_ykrk");//移库入库
		list.add("delete from t_crm_ykrkproduct");//移库入库明细
		
		list.add("delete from t_crm_yufuk");//预付款
		list.add("delete from t_crm_yufuto");//预付冲应付
		list.add("delete from t_crm_yufutodesc");//预付冲应付明细
		
		list.add("delete from t_crm_yushouk");//预收款
		list.add("delete from t_crm_yushouto");//预收冲应收
		list.add("delete from t_crm_yushoutodesc");//预收冲应收明细
		Session session=getSession();
		
		for(String string:list){
			session.createSQLQuery(string).executeUpdate();
		}
		
	}
}